package com.ruoyi.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPythonUrlEntity;
import com.ruoyi.system.domain.vo.PageVo;
import com.ruoyi.system.mapper.ISysPythonUrlMapper;
import com.ruoyi.system.service.IMpSysPythonUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-01-19
 */
@Service
public class MpSysPythonUrlServiceImpl  implements IMpSysPythonUrlService {
    @Autowired
    private ISysPythonUrlMapper iSysPythonUrlMapper;
    @Autowired
    private RedisCache redisCache;
    @Override
    public Integer saveAndUpdateUrlService(SysPythonUrlEntity sysPythonUrlEntity) {
        if (sysPythonUrlEntity.getId() == null) {
//            如是查询则分页,将分页数据记录在缓存中
            Long redisName =redisCache.getExpire(sysPythonUrlEntity.getName());
            if (redisName<0&&sysPythonUrlEntity.getUrl().contains("search")) {
                int pagebili = 1;
                redisCache.setCacheObject(sysPythonUrlEntity.getName(), pagebili);
//                redisCache.setCacheList()
                return iSysPythonUrlMapper.insert(sysPythonUrlEntity);
            }
            return iSysPythonUrlMapper.insert(sysPythonUrlEntity);
        }else {
            return iSysPythonUrlMapper.updateById(sysPythonUrlEntity);
        }
    }
    @Override
    public PageInfo<SysPythonUrlEntity> searchSysPythonUrl(SysPythonUrlEntity sysPythonUrlEntity, PageVo pageVo){
        PageHelper.startPage(pageVo.getPageNum(),pageVo.getPageSize());
        List<SysPythonUrlEntity> sysPythonUrlEntities = iSysPythonUrlMapper.selectList(new LambdaQueryWrapper<SysPythonUrlEntity>()
                .like(sysPythonUrlEntity!=null&&StringUtils.isNotEmpty(sysPythonUrlEntity.getName()),SysPythonUrlEntity::getName,sysPythonUrlEntity.getName())
                .eq(sysPythonUrlEntity!=null&&sysPythonUrlEntity.getId()!=null,SysPythonUrlEntity::getId,sysPythonUrlEntity.getId())
                .eq(SysPythonUrlEntity::getDelStatus,0)
        );
        sysPythonUrlEntities.forEach(sysPythonUrlEntity1 -> {
            if (sysPythonUrlEntity1.getStatus() == 0){
                sysPythonUrlEntity1.setStatusBoolean(true);
            }else {
                sysPythonUrlEntity1.setStatusBoolean(false);
            }
        });
//        redisCache.expire();
        return new PageInfo<>(sysPythonUrlEntities);
    }

    @Override
    public Integer onUrl(Integer id,Integer status) {
        SysPythonUrlEntity sysPythonUrlEntity = iSysPythonUrlMapper.selectById(id);
        sysPythonUrlEntity.setStatus(status);
        return iSysPythonUrlMapper.updateById(sysPythonUrlEntity);
    }

    @Override
    public Integer delUrl(Integer id) {
        SysPythonUrlEntity sysPythonUrlEntity = iSysPythonUrlMapper.selectById(id);
        sysPythonUrlEntity.setDelStatus(1);
        redisCache.deleteObject(sysPythonUrlEntity.getName());
        return iSysPythonUrlMapper.updateById(sysPythonUrlEntity);
    }


}
